# -*- coding: utf-8 -*-

from django.db import models
from users.models import UserProfile
import time

# Create your models here.
class Event(models.Model):
    @staticmethod
    def recordOnlyFields():
        return ('umpires', 'temperature', 'humidity', 'windSpeed')
    @staticmethod
    def signupOnlyFields():
        return ('type', 'fee', 'fieldAddress', 'fieldLat', 'fieldLng', 'signupStartDateTime', 'signupEndDateTime', 'runnerLimit')
    
    TYPE_FORMAL = 'f'
    TYPE_PRACTICE = 'p'
    TYPE_CHOICES = (
                    (TYPE_FORMAL, 'formal'),
                    (TYPE_PRACTICE, 'practice'),
                    )
    # general fields
    name = models.CharField(max_length=200)
    date = models.DateTimeField()
    endDate = models.DateTimeField(blank=True, null=True)
    fee = models.FloatField(help_text='in NTD, set 0 for free')
    fieldName = models.CharField(max_length=200)
    fieldAddress = models.CharField(max_length=300, help_text='enter full address for google map displaying')
    fieldLat = models.FloatField()
    fieldLng = models.FloatField()
    type = models.CharField(max_length=2,
                            choices = TYPE_CHOICES,
                            default = TYPE_FORMAL)
    country = models.CharField(max_length=200)
    county = models.CharField(max_length=200)

    # signup only fields
    signupStartDateTime = models.DateTimeField()
    signupEndDateTime = models.DateTimeField()
    runnerLimit = models.IntegerField() # 0 for unlimited

    # record fields
    umpires = models.CharField(max_length=300, blank=True, null=True)
    temperature = models.FloatField(blank=True, null=True, help_text='°c')
    humidity = models.FloatField(blank=True, null=True, help_text='kg/m^3')
    windSpeed = models.FloatField(blank=True, null=True, help_text='m/s')
    
    def __unicode__(self):
        out_str = '' + str(self.date) + " " + self.name
        return out_str
    
class Participant(models.Model):
    """
    Event Signup admin-panel will exclude displaying fields declared in recordOnlyFields,
    Event Record admin-panel will exclude displaying fields declared in signupOnlyField.  
    """
    @staticmethod
    def signupOnlyFields():
        return ('signupKey','bday', 'pid', 'email', 'phoneNum', 'emgcyContact', 'emgcyContPhone', 'estimateTime', 'signupDateTime')
    @staticmethod
    def recordOnlyFields():
        return ('serial', 'totalRank', 'genderRank', 'record', 'avgspeed') 
    
    event = models.ForeignKey(Event)
    user = models.ForeignKey(UserProfile, blank=True, null=True)
    
    # general fields
    name = models.CharField(max_length=100)
    gender = models.CharField(max_length=6)
    nationality = models.CharField(max_length=100, blank=True, null=True)
    
    # sign only fields
    signupKey = models.CharField(max_length=50)
    bday = models.DateField(blank=True, null=True)
    pid = models.CharField(max_length=100, blank=True, null=True) # personal id
    email = models.CharField(max_length=250, blank=True, null=True)
    phoneNum = models.CharField(max_length=50, blank=True, null=True)
    emgcyContact = models.CharField(max_length=200, blank=True, null=True)
    emgcyContPhone = models.CharField(max_length=50, blank=True, null=True)
    estimateTime = models.CharField(max_length=100, blank=True, null=True)
    signupDateTime = models.DateTimeField() # this field should be filled by server only
    
    # record only fields
    serial = models.IntegerField(blank=True, default=None, null=True, help_text='0 means DNS') # serial number in event    
    totalRank = models.IntegerField(blank=True, default=None, null=True, help_text='0 means DNF')
    genderRank = models.IntegerField(blank=True, default=None, null=True, help_text='0 means DNF')
    record = models.FloatField(blank=True, default=None, null=True, help_text='will be converted into seconds')   # record in seconds
    avgspeed = models.FloatField(blank=True, default=None, null=True, help_text='will be converted into seconds')  # record in seconds
    
    #field_map = ['number', 'name', 'gender', 'totalRank', 'genderRank', 'records', 'avgspeed']
    #field_type = [conv_int, conv_str, conv_gender, conv_int, conv_int, conv_date, conv_date]
        
    def __unicode__(self):
        out_str = '' + self.event.name + "_" + str(self.serial) + "_" + self.name
        return out_str
    
    def records_in_minutes_str(self):
        return  time.strftime('%H:%M:%S', time.gmtime(self.records))
    
    def speed_in_minutes_str(self):
        return  time.strftime('%H:%M:%S', time.gmtime(self.avgspeed))